package com.smec.remes.module.register.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hwangjr.rxbus.Bus;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.SmecRxBus;
import com.smec.moblie.BR;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutRecyclerviewContractBinding;
import com.smec.moblie.databinding.LayoutRecyclerviewMantrBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.bean.AbleMatnr;
import com.smec.remes.bean.SearchContract;
import com.smec.remes.module.register.SmecRegisterFragment;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/9.
 */

public class SmecContractAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {

    private ArrayList<SearchContract> arrayList=new ArrayList<>();
    private Context mcontext;
    protected Bus mRxbus;

    public SmecContractAdapter(Context context) {
        this.mcontext=context;
        mRxbus= SmecRxBus.get();
        mRxbus.register(this);
    }

    public void getList(ArrayList<SearchContract> list,String status){
        if(status.equals("onRefresh")){
            arrayList=list;
        }else {
            arrayList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearList(){
        arrayList.clear();
        notifyDataSetChanged();
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutRecyclerviewContractBinding layoutRecyclerviewContractBinding=DataBindingUtil.inflate
                (LayoutInflater.from(parent.getContext()), R.layout.layout_recyclerview_contract,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                SmecBaseRecyclerViewHolder(layoutRecyclerviewContractBinding.getRoot());
        smecBaseRecyclerViewHolder.setViewType(viewType);
        smecBaseRecyclerViewHolder.setBinding(layoutRecyclerviewContractBinding);
        return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, final int position) {
        final LayoutRecyclerviewContractBinding layoutRecyclerviewContractBinding= (LayoutRecyclerviewContractBinding) holder.getBinding();
        layoutRecyclerviewContractBinding.setVariable(BR.contract,arrayList.get(position));
        layoutRecyclerviewContractBinding.llAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Activity activity = (Activity)mcontext;
                activity.finish();
                SearchContract searchContract=arrayList.get(position);
                mRxbus.post("Contract",searchContract);
            }
        });
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }
}
